// 数据回显ajax
$.ajax({
    type: "post",
    url: "/js/showteapagejsgetlocal",
    data: null,
    dataType: "json",
    success: function (data1) {
        let showteaobj = JSON.parse(data1);
        let teatypearr = ["绿茶", "黄茶", "乌龙茶", "红茶", "黑茶", "白茶", "花茶"];
        document.getElementsByClassName("bigimg")[0].innerHTML = `<img src="../imgs/${showteaobj[0].teaimg}" alt="">`;
        document.getElementsByClassName("smallimg")[0].innerHTML = `<img src="../imgs/${showteaobj[0].teaimg}" alt="">`;
        document.getElementsByClassName("showteaname")[0].innerHTML = `${showteaobj[0].teaname}`;
        document.getElementsByClassName("showteaprice")[0].innerHTML = `￥${showteaobj[0].teaprice}`;
        document.getElementsByClassName("showteadescribe")[0].innerHTML = `${showteaobj[0].teadescribe}`;
        document.getElementsByClassName("showteatype")[0].innerHTML = `${teatypearr[showteaobj[0].teatype]}`;

        // 发送ajax请求获取相关的茶
        $.ajax({
            type: "post",
            url: "/js/showTeaPageJsGetXGlocal",
            data: data1,
            dataType: "json",
            success: function (teaarr) {
                let teaArr = teaarr;//本地存储
                let showteaPage = "";
                let xgsparr = document.getElementsByClassName("xgsp_show")[0];
                for (let i = 0; i < teaArr.length; i++) {
                    showteaPage += `<div>
                                        <div class="xgsp_show_img">
                                        <img src="../imgs/${teaArr[i].teaimg}" alt="">
                                        </div>
                                        <div class="xgsp_show_name">${teaArr[i].teaname}</div>
                                        <div class="xgsp_show_price">￥${teaArr[i].teaprice}</div>
                                    </div>`;
                }
                xgsparr.innerHTML = showteaPage;
            }
        })

        document.getElementById("addShopCar").onclick = function () {

            let shopcar = localStorage.shopCar;

            if (shopcar == "" || shopcar == null || shopcar == undefined || shopcar == []) {
                localStorage.shopCar = data1;
                alert("添加至购物车成功");
                window.location.href = "./shopcar.html";
            } else {
                let bool = false;
                let shopcararr = JSON.parse(shopcar);
                for (let i = 0; i < shopcararr.length; i++) {
                    if (shopcararr[i].teaid == showteaobj[0].teaid) {
                        bool = true;
                        break;
                    }
                }
                if (bool) {
                    alert("该茶叶已添加至购物车");
                } else {
                    shopcararr.push(showteaobj[0]);
                    localStorage.shopCar = JSON.stringify(shopcararr);
                    alert("添加至购物车成功");
                    window.location.href = "./shopcar.html";
                }
            }


        }
    }
})
// 添加购物车


